package commun;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.*;

import Partie1.BernoulliParser;



public class Review {
	
	
	
	public int id;
	public ReviewType realType;
	public double propaPositive=0;
	public double propaNegative=0;
	public long nbWords;
	public Map<String, Double> wordsMap;//pour chaque mot, le nombre d'occurrence dans ce document
	public Review(ArrayList<String> lst){
		wordsMap =new  HashMap<String, Double>();
		nbWords = 0;
		this.parseReview(lst);
	}
	
	
	
	private void parseReview(ArrayList<String> lst)
	{
		//analyse type
		this.getTypeID(lst.get(0));
		Map<String, Integer> stopWords =new  HashMap<String, Integer>();
		for(int i = 0; i < Partie1.BernoulliParser.stopWords.length; i++ )
    	{
    		stopWords.put(Partie1.BernoulliParser.stopWords[i], 0);
    	}
		//get words
		for(int i=1; i < lst.size() - 1; i++)
		{
			String strLine = lst.get(i).toLowerCase().trim();

	    	for(int j=0; j < Partie1.BernoulliParser.points.length; j++)
	    	{
				try{
					strLine = strLine.replace(Partie1.BernoulliParser.points[j], ' ');
	    		}catch(Exception e)
	    		{
	    		  System.out.println("index  "+j);
	    		}
	    	}
	    	String ws[] = strLine.split(" ");
	    	//nbWords += ws.length;
	        for(int j =0; j< ws.length; j++)
	        {
	        	if(!stopWords.containsKey(ws[j]) && ws[j].length() > 0)
	        	{
	        		nbWords++;
	        		if(this.wordsMap.containsKey(ws[j]))
		        	{
	        			double val = (Double)wordsMap.get(ws[j]);
		        		val+=1.0;
		        		wordsMap.put(ws[j], val);
		        	}
		        	else
		        	{
		        		wordsMap.put(ws[j], 1.0);
		        	}
	        	}
	        }
		}
		
	}
	private void getTypeID(String str)
	{
		String regEx2= "id='([0-9]*)'"; 
		String regEx1= "class='([a-zA-Z]*)'"; 
		Pattern p=Pattern.compile(regEx1);
		Matcher m=p.matcher(str);
		String type="";
	    while (m.find()) {
		   type = m.group(1).toString();
		  // System.out.println("type:"+type);
		}
	    if(type.equalsIgnoreCase("negative")){
	    	this.realType = ReviewType.Negative;
	    }else  if(type.equalsIgnoreCase("positive")){
	    	this.realType = ReviewType.Positive;
		}else {
			this.realType = ReviewType.Unknown;
		}
	    
		Pattern p2=Pattern.compile(regEx2);
		Matcher m2=p2.matcher(str);
		String idString="";
	    while (m2.find()) {
		   idString = m2.group(1).toString();
		  // System.out.println("id:"+idString);
		}
	    this.id = Integer.valueOf(idString);
	}
}
